package com.service.Impl;

import com.entity.Plate;
import com.exception.GlobalException;
import com.mapper.PlateMapper;
import com.service.PlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: PlateServiceImpl
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/05/23 11:21
 */
@Service
public class PlateServiceImpl implements PlateService {
    @Autowired
    private PlateMapper plateMapper;

    @Override
    public void addPlate(Plate plate) {
        //1. 查询板块是否存在
        boolean plateExist = isPlateExist(plate.getPlateName());
        if (plateExist) {
            throw new RuntimeException("板块已存在");
        }
        //2. 如果板块不存在就添加板块
        plateMapper.addPlate(plate);
    }

    @Override
    public void deletePlate(Integer plateId) {
        try {
            plateMapper.deletePlate(plateId);
        } catch (Exception e) {
            throw new GlobalException(1001,"板块删除失败");
        }
    }

    @Override
    public void updatePlate(Plate plate) {
        try {
            plateMapper.updatePlate(plate);
        } catch (Exception e) {
            throw new GlobalException(1001,"板块更新失败");
        }
    }

    @Override
    public List<Plate> getAllPlate() {
        return plateMapper.getAllPlate();
    }

    @Override
    public Plate getPlateById(Integer plateId) {
        return plateMapper.getPlateById(plateId);
    }

    @Override
    public boolean isPlateExist(String plateName) {
        return plateMapper.isPlateExist(plateName);
    }

    @Override
    public List<Plate> selectHotPlates() {
        return plateMapper.selectHotPlates();
    }
}
